package com.example.databindingdemo;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;

import com.example.databindingdemo.databinding.ActivityBindingadapterBinding;
import com.example.databindingdemo.databinding.ActivityObservablefieldBinding;
import com.example.databindingdemo.model.ObservableFieldModel;

public class ObservableFieldActivity extends AppCompatActivity {

    private ObservableFieldModel observableFieldModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityObservablefieldBinding activityObservablefieldBinding = DataBindingUtil.setContentView(this,R.layout.activity_observablefield);
        observableFieldModel = new ObservableFieldModel();
        activityObservablefieldBinding.setObservableFieldModel(observableFieldModel);
        ((EditText)findViewById(R.id.et)).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                Log.e("双向绑定",observableFieldModel.getUserName());
            }
        });
    }
}
